<%@ page language="java" contentType="text/html; charset=UTF-8"	 pageEncoding="UTF-8"%>
<%@ include file="../include/laypublic.jsp"%>
<!DOCTYPE html>
<html>
<head>
</head>
  <title>人员管理</title>
  <style type="text/css">
  	.layui-form-select dl{
		max-height: 150px; // 原值为300，调节高度建议为50的倍数
	}
	.layui-form-label{width: 130px;}
	.layui-form-item {
	    margin-bottom: 5px;
	    clear: both;
	    *zoom: 1;
	}
  </style>
</head>
<body>
  <div class="layui-fluid">   
    <div class="layui-card">
      <!-- 查询区域  -->
      <div id="query-form" class="layui-form layui-card-header layuiadmin-card-header-auto">
        <div class="layui-form-item">
        	<div class="layui-inline">
            	<label class="layui-form-label">公司名称</label>
            	<div class="layui-input-inline">
	              	<select name="orgId" id="query-select-orgId" lay-filter="query_select_orgId" lay-search>
	              		<c:if test="${not empty orgInfos }">
	              			<c:forEach items="${orgInfos}" var="org">
	              				<option value="${org.id }" <c:if test="${org.id == bindOrgId }">selected</c:if>>${org.compName }</option>
		            		</c:forEach>
	              		</c:if>
	              	</select>
            	</div>
          </div>
          <div class="layui-inline">
            <label class="layui-form-label">人员名称</label>
            <div class="layui-input-inline">
              <input type="text" name="staffName" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
          </div>
          <div class="layui-inline">
            <label class="layui-form-label">人员手机号</label>
            <div class="layui-input-inline">
              <input type="text" name="staffPhone" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
          </div>
          <div class="layui-inline">
            <label class="layui-form-label">在职状态</label>
            <div class="layui-input-inline">
              <select id="query-form-state" name="state">
              	<option value="">请选择-在职状态</option>
              	<option value="1">在职中</option>
              	<option value="0">已离职</option>
              </select>
            </div>
          </div>
          <shiro:hasPermission name="LIST_STAFF">
	          <div class="layui-inline">
	            <button class="layui-btn layuiadmin-btn-admin" lay-submit lay-filter="LAY-staff-search">
	              <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>查询
	            </button>
	            <button class="layui-btn layuiadmin-btn-admin" lay-submit lay-filter="LAY-user-back-reset">
	              <i class="layui-icon layui-icon-fonts-clear  layuiadmin-button-btn"></i>重置
	            </button>
	          </div>
          </shiro:hasPermission>
        </div>
      </div>
  	  <!-- 按钮区域  -->
      <div class="layui-card-body">
        <div style="padding-bottom: 10px;">
          <shiro:hasPermission name="SAVE_STAFF">
          	<button class="layui-btn layuiadmin-btn-admin " data-type="add">
          	 	<i class="layui-icon layui-icon-add-1 layuiadmin-button-btn"></i>新增
          	</button>
          </shiro:hasPermission>
        </div>
        <!-- 数据表单 -->
        <table id="LAY-staff-manage" lay-filter="LAY-staff-manage"></table>
        
        <div class="layui-form" lay-filter="add_form" id="add_form" style="padding: 20px 30px 0 0;display:none">
		    <div class="layui-form-item">
		    	<div class="layui-inline">
			      	<label class="layui-form-label">公司名称</label>
			      	<div class="layui-input-inline">
			        	<input type="text" id="add_orgName" style="border:0px;width:220px;" readonly="readonly" class="layui-input">
			        	<input type="hidden" name="orgId" id="add_orgId" value="${bindOrgId }" />
			      	</div>
		      	</div>
	      	</div>
	      	<div class="layui-form-item">
		    	<div class="layui-inline">
			      	<label class="layui-form-label">人员姓名</label>
			      	<div class="layui-input-inline">
			        	<input type="text" name="staffName" maxlength="30" lay-verify="required" placeholder="请输入人员姓名" autocomplete="off" class="layui-input">
			      	</div>
		      	</div>
		    </div>
			<div class="layui-form-item">
		    	<div class="layui-inline">
			      	<label class="layui-form-label">人员手机号</label>
			      	<div class="layui-input-inline">
			        	<input type="text" name="staffPhone" maxlength="11" lay-verify="required|phone" placeholder="请输入人员手机号" autocomplete="off" class="layui-input">
			      	</div>
		      	</div>
	      	</div>
		    <div class="layui-form-item layui-hide">
		      	<input type="button" lay-submit lay-filter="add_form_submit" id="add_form_submit" value="确认">
		    </div>
	  	</div>
	  	
	  	<!-- 编辑窗口 -->
        <div class="layui-form" lay-filter="edit_form" id="edit_form" style="padding: 20px 30px 0 0;display:none">
        	<input type="hidden" name="id"/>
        	<input type="hidden" name="orgId"/>
        	<input type="hidden" name="staffId"/>
		    <div class="layui-form-item">
		    	<div class="layui-inline">
			      	<label class="layui-form-label">人员Id</label>
			      	<div class="layui-input-inline">
			        	<input type="text" name="staffCode" style="border:0px;" readonly="readonly" class="layui-input">
			      	</div>
		      	</div>
	      	</div>
	      	<div class="layui-form-item">
		    	<div class="layui-inline">
			      	<label class="layui-form-label">人员姓名</label>
			      	<div class="layui-input-inline">
			        	<input type="text" name="staffName" maxlength="30" lay-verify="required" placeholder="请输入人员姓名" autocomplete="off" class="layui-input">
			      	</div>
		      	</div>
		    </div>
			<div class="layui-form-item">
		    	<div class="layui-inline">
			      	<label class="layui-form-label">人员手机号</label>
			      	<div class="layui-input-inline">
			        	<input type="text" name="staffPhone" maxlength="11" lay-verify="required|phone" placeholder="请输入人员手机号" autocomplete="off" class="layui-input">
			      	</div>
		      	</div>
	      	</div>
		    <div class="layui-form-item layui-hide">
		      <input type="button" lay-submit lay-filter="edit_form_submit" id="edit_form_submit" value="确认">
		    </div>
		    
		    <!-- 岗位明细table -->
        	<table id="LAY-staff-office-detail" lay-filter="LAY-staff-office-detail"></table>
	  	</div>
	  	
	  	<!-- 新增明细window -->
	  	<div class="layui-form" lay-filter="add_detail_from" id="add_detail_from" style="padding: 20px 30px 0 0;display:none">
	  		<input type="hidden" name="staffId"/>
		    <div class="layui-form-item">
		      	<label class="layui-form-label">所属公司</label>
		      	<div class="layui-input-inline">
			      	<select name="orgId" id="add_detail_select_orgId" lay-filter="add_detail_select_orgId" lay-search>
			      		<c:if test="${not empty orgInfos }">
	              			<c:forEach items="${orgInfos}" var="org">
	              				<option value="${org.id }" <c:if test="${org.id == bindOrgId }">selected</c:if>>${org.compName }</option>
		            		</c:forEach>
	              		</c:if>
              		</select>
		      	</div>
		    </div>
		   <div class="layui-form-item">
	    		<label class="layui-form-label">所属部门</label>
	      		<div class="layui-input-inline">
	      			<select name="deptId" id="add_selectDept" lay-filter="add_selectDept" lay-verify="required" lay-search>
					</select>
	      		</div>
		    </div>
		    <div class="layui-form-item">
	    		<label class="layui-form-label">所属岗位</label>
	      		<div class="layui-input-inline">
	      			<select name="postId" id="add_selectPost" lay-verify="required" lay-search>
					</select>
	      		</div>
		    </div>
		    <div class="layui-form-item">
	    		<label class="layui-form-label">生效开始时间</label>
	      		<div class="layui-input-inline">
	        		<input type="text" class="layui-input" id="add-laydate-start" lay-verify="required" name="startTime" readonly placeholder="请选择生效开始时间">
	      		</div>
		    </div>
		    <div class="layui-form-item">
		    	<label class="layui-form-label">生效结束时间</label>
		    	<div class="layui-input-inline">
		    		<input type="text" class="layui-input" id="add-laydate-end" name="endTime" readonly placeholder="请选择生效结束时间">
		    	</div>
		  	</div>
		    <div class="layui-form-item layui-hide">
		      <input type="button" lay-submit lay-filter="add_detail_from_submit" id="add_detail_from_submit" value="确认">
		    </div>
	  	</div>
	  	
	  	<!-- 编辑明细window -->
	  	<div class="layui-form" lay-filter="edit_detail_from" id="edit_detail_from" style="padding: 20px 30px 0 0;display:none">
	  		<input type="hidden" name="id" />
	  		<input type="hidden" name="staffId"/>
		    <div class="layui-form-item">
		      	<label class="layui-form-label">所属公司</label>
		      	<div class="layui-input-inline">
			      	<select name="orgId" lay-filter="edit_detail_select_orgId" lay-search>
			      		<c:if test="${not empty orgInfos }">
	              			<c:forEach items="${orgInfos}" var="org">
	              				<option value="${org.id }">${org.compName }</option>
		            		</c:forEach>
	              		</c:if>
              		</select>
		      	</div>
		    </div>
		   <div class="layui-form-item">
	    		<label class="layui-form-label">所属部门</label>
	      		<div class="layui-input-inline">
	      			<select name="deptId" id="edit_selectDept" lay-filter="edit_selectDept" lay-verify="required" lay-search>
					</select>
	      		</div>
		    </div>
		    <div class="layui-form-item">
	    		<label class="layui-form-label">所属岗位</label>
	      		<div class="layui-input-inline">
	      			<select name="postId" id="edit_selectPost" lay-verify="required" lay-search>
					</select>
	      		</div>
		    </div>
		    <div class="layui-form-item">
	    		<label class="layui-form-label">生效开始时间</label>
	      		<div class="layui-input-inline">
	        		<input type="text" class="layui-input" id="edit-laydate-start" lay-verify="required" name="startTime" readonly placeholder="请选择生效开始时间">
	      		</div>
		    </div>
		    <div class="layui-form-item">
		    	<label class="layui-form-label">生效结束时间</label>
		    	<div class="layui-input-inline">
		    		<input type="text" class="layui-input" id="edit-laydate-end" name="endTime" readonly placeholder="请选择生效结束时间">
		    	</div>
		  	</div>
		    <div class="layui-form-item layui-hide">
		      <input type="button" lay-submit lay-filter="edit_detail_from_submit" id="edit_detail_from_submit" value="确认">
		    </div>
	  	</div>
	  	
	  	<!-- 详情窗口 -->
        <div class="layui-form" lay-filter="detail_form" id="detail_form" style="padding: 20px 30px 0 0;display:none">
	      	<div class="layui-form-item">
		    	<div class="layui-inline">
			      	<label class="layui-form-label">人员ID</label>
			      	<div class="layui-input-inline">
			        	<input type="text" name="staffCode" style="border:0px;" readonly="readonly" class="layui-input">
			      	</div>
		      	</div>
		    </div>
	      	<div class="layui-form-item">
		    	<div class="layui-inline">
			      	<label class="layui-form-label">人员姓名</label>
			      	<div class="layui-input-inline">
			        	<input type="text" name="staffName" style="border:0px;" readonly="readonly" class="layui-input">
			      	</div>
		      	</div>
		    </div>
			<div class="layui-form-item">
		    	<div class="layui-inline">
			      	<label class="layui-form-label">人员手机号</label>
			      	<div class="layui-input-inline">
			        	<input type="text" name="staffPhone" style="border:0px;" readonly="readonly" class="layui-input">
			      	</div>
		      	</div>
	      	</div>
	      	<div class="layui-form-item">
		    	<div class="layui-inline">
			      	<label class="layui-form-label">在职状态</label>
			      	<div class="layui-input-inline">
			        	<input type="text" name="state" id="detail_state" style="border:0px;" readonly="readonly" class="layui-input">
			      	</div>
		      	</div>
		    </div>
		    <fieldset class="layui-elem-field layui-field-title fieldset">
			  <legend class="layui-btn layui-legend" style="text-align:left;cursor:default;">岗位信息  ></legend>
				  <div style="margin-left:20px;">
			    	<table class="layui-table layuiadmin-page-table" lay-skin="line">
	                  <thead>
	                    <tr>
	                      <th>所属公司</th>
	                      <th>所属部门</th>
	                      <th>所属岗位</th>
	                      <th>生效开始时间</th>
	                      <th>生效结束时间</th>
	                      <th>岗位状态</th>
	                    </tr> 
	                  </thead>
	                  <tbody id="detail_tbody">
	                  </tbody>
	            	</table>
            	</div>
            </fieldset>
		    <div class="layui-form-item layui-hide">
		      <input type="button" lay-submit lay-filter="detail_form_submit" id="detail_form_submit" value="确认">
		    </div>
	  	</div>
	  	
      </div>
    </div>
  </div>
  	<shiro:hasPermission name="SAVE_STAFF_OFFICE">
	<script type="text/html" id="table-toolbar-addOffice">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-sm" lay-event="addOffice">
				<i class="layui-icon layui-icon-add-1 layuiadmin-button-btn"></i>新增就职岗位
	        </button>
       	</div>
  	</script>
	</shiro:hasPermission>
  <script>
  	layui.config({
    	base: '${ctx}/resources/layuiadmin/' //静态资源所在路径
  	}).extend({
    	index: 'lib/index' //主入口模块
  	}).use(['index', 'table', 'set', 'transfer', 'laydate'], function(){
    	var $ = layui.$
    	,form = layui.form
    	,table = layui.table
    	,laydate = layui.laydate;
    	
    	// 表单
    	table.render({
      		elem: '#LAY-staff-manage'
      		,url: basePath + '/staff/queryDataByCondition' //请求数据接口
      		,cols: [[
        		{type: 'checkbox', fixed: 'left'}
        		,{field: 'staffCode', title: '人员ID',width:110}
        		,{field: 'staffName', title: '人员姓名',width:110}
        		,{field: 'staffPhone', title: '人员手机号',width:120}
        		,{field: 'orgName', title: '公司名称',width:280}
        		,{field: 'deptName', title: '部门',width:150, templet: function(d){
    		  		return d.deptName||global_null_tip;
          		   }
        		}
        		,{field: 'postName', title: '岗位',width:170, templet: function(d){
    		  		return d.postName||global_null_tip;
          		}}
        		,{field: 'state', title: '在职状态',width:90,fixed: 'right', templet: function (d) {
	        			if(d.state==1){
	          				return "在职中";
	          			}else{return '<span class="layui-badge layui-bg-cyan">已离职</span>' ;}
		        	}
 		    	}
        		,{field: 'officeState', title: '岗位状态',width:90, fixed: 'right',templet: function(d){
	  	            	if(d.officeState == 1){
	      					return "生效中";
	      				}else{
	      					return '<span class="layui-badge layui-bg-cyan">已失效</span>' ;
	  					}
  					}
	            }
        		,{field: 'addTime', title: '创建时间',width:170, templet: function(d){
            		  	return layui.util.toDateString(d.addTime,"yyyy-MM-dd HH:mm:ss");
	              	}
	             }
        		,{title: '操作', width: 240, align:'center', fixed: 'right', templet: function(d){
      	  				var optStr ="";
      	  				<shiro:hasPermission name="VIEW_STAFF">
      	  					optStr += '<a class="layui-btn layui-btn-xs" lay-event="view"><i class="layui-icon layui-icon-form"></i>查看</a>';
  	  	  				</shiro:hasPermission>
      	  				<shiro:hasPermission name="SAVE_STAFF">
      	 					optStr += '<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑人员</a>';
  	  	  				</shiro:hasPermission>
  	  	  				<shiro:hasPermission name="UPDATE_STATUS_STAFF">
		  	  	  			if(d.state == 0){//状态为停用
      	 						optStr += '<a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="use"><i class="layui-icon layui-icon-ok"></i>在职</a>';
		   	        		}else if(d.state == 1){//状态为启用
      	 						optStr += '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="freeze"><i class="layui-icon layui-icon-pause"></i>离职</a>';
		   	        		}
						</shiro:hasPermission>
			  		  	return optStr;
        			}
        		}
      			]]
      		,page: true
      		,limit: 10
    	});
    
    	<shiro:hasPermission name="LIST_STAFF">
	    	//监听搜索
	    	form.on('submit(LAY-staff-search)', function(data){
		      	//执行重载
		      	table.reload('LAY-staff-manage', {page: {curr: 1},
		        	where: trimData(data)
		      	});
	    	});
	    	//重置清空
		    form.on('submit(LAY-user-back-reset)', function(data){
		    	reset_form('query-form');
		    	//额外处理select
				$("#query-form-state").val('');
				if('${bindOrgId}'==null||'${bindOrgId}'==""){
					$("#query-select-orgId").val('${superOrgId}');
				}else{
					$("#query-select-orgId").val('${bindOrgId}');
				}
				form.render();
		    });
    	</shiro:hasPermission>
    
    	// 监听操作列
    	table.on('tool(LAY-staff-manage)', function(obj){
      		var data = obj.data;
      		if(obj.event === 'use'){
          		layer.confirm('确定在职？',{icon:3, title :'提示'}, function(index){
          			$.ajax({
  						type : 'post',
  						url : basePath + '/staff/updateStatus?id=' + data.staffId,
  						success:function(result){
  							if(result.success){
  								table.reload('LAY-staff-manage'); //数据刷新
  								layer.close(index);
  							}else{
  								layer.msg(result.msg,{icon: 7});
  				  			}
  						}
  					});
          		});
      		}else if(obj.event === 'freeze'){
          		layer.confirm('确定离职？',{icon:3, title :'提示'}, function(index){
          			$.ajax({
  						type : 'post',
  						url : basePath + '/staff/updateStatus?id=' + data.staffId,
  						success:function(result){
  							if(result.success){
  								table.reload('LAY-staff-manage'); //数据刷新
  								layer.close(index);
  							}else{
  								layer.msg(result.msg,{icon: 7});
  				  			}
  						}
  					});
          		});
      		} else if(obj.event === 'edit'){
      			form.val('edit_form',data); // 填充原值
      			$('input[name=staffId]').val(data.staffId);
      	     	//参数集合table渲染
      	        table.render({
      	          elem: '#LAY-staff-office-detail'
      	          ,url: basePath + '/staff/queryDataByStaffId?staffId='+data.staffId //请求数据接口
      	          ,toolbar:'#table-toolbar-addOffice'
      	          ,defaultToolbar: []
      	          ,cols: [[
					{type: 'checkbox', fixed: 'left'}
      	            ,{field: 'orgName', title: '所属公司'}
      	            ,{field: 'deptName', title: '所属部门'}
      	            ,{field: 'postName', title: '所属岗位', templet: function(d){
	        		  		return d.postName||global_null_tip;
	              		}
      	            }
      	            ,{field: 'startTime', title: '生效开始时间', templet: function(d){
            		  		return layui.util.toDateString(d.startTime,"yyyy-MM-dd HH:mm:ss");
	              		}
      	            }
      	            ,{field: 'endTime', title: '生效结束时间', templet: function(d){
      	            		if(d.endTime != null && d.endTime != ''){
        		  				return layui.util.toDateString(d.endTime,"yyyy-MM-dd HH:mm:ss");
      	            		}else{
        		  				return global_null_tip;
      	            		}
              			}
      	            }
      	            ,{field: 'officeState', title: '岗位状态', templet: function(d){
	      	            	if(d.officeState == 1){
	          					return "生效中";
	          				}else{
	          					return '<span class="layui-badge layui-bg-cyan">已失效</span>' ;
          					}
          				}
      	            }
      	            ,{title: '操作', width: 171, align:'center', fixed: 'right', templet: function(d){
      	    	      	  	var optStr ="";
      	    	  	  	  	<shiro:hasPermission name="SAVE_STAFF_OFFICE">
      	    	  	    		 optStr += '<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="editDetail"><i class="layui-icon layui-icon-edit"></i>编辑</a>';
      	    	  		  	</shiro:hasPermission>
      	    	  	  	  	<shiro:hasPermission name="DELETE_STAFF_OFFICE">
      	    	  	    		 optStr += '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delDetail"><i class="layui-icon layui-icon-delete"></i>删除</a>';
      	    	  		  	</shiro:hasPermission>
      	    	  		  	return optStr;
      	            	}
      	            }
      	          ]]
      	          ,page: true
      	          ,limit: 5
      	          ,limits:[5,10,20,30,50]
      	        });
      			layer.open({
            		type: 1
            		,title: '编辑人员'
            		,content: $('#edit_form')
            		,area: layui.admin.screen() < 2 ? ['80%', '620px'] : ['1200px','620px']
            		,btn: ['确定', '取消']
            		,btnAlign: 'c'
            		,yes: function(index, layero){
          	  			var submitID = 'edit_form_submit'
          		  		,submit = layero.contents().find('#'+ submitID);
          	  			//监听提交
          	  			form.on('submit('+ submitID +')', function(data){
                			var field = data.field; //获取提交的字段
                			//提交 Ajax 成功后，静态更新表格中的数据
                			$.ajax({
                    			type : 'post',
  								url : basePath + '/staff/save',
                    			data: field,
  								success:function(result){
  									if(result.success){
                						table.reload('LAY-staff-manage'); //数据刷新
                						layer.close(index); //关闭弹层
  									}else{
  										layer.msg(result.msg,{icon: 7});
  					  				}
  								}
                			});
              			});  
              		submit.trigger('click');
           		}
          	});
       	} else if(obj.event === 'view'){
  			form.val('detail_form',data); // 填充原值
  			if(data.state == 1){
  				$("#detail_state").val('在职中');
  			}else if(data.state == 0){
  				$("#detail_state").val('已离职');
  			}
  			$('#detail_tbody').html('');
  			$.ajax({
					type : 'post',
					url : basePath + '/staff/queryById?id=' + data.staffId,
					success:function(result){
						if(result.success){
							var strBuffer = "";
							if(result.entity.staffOfficeVos.length > 0){
								for(var i in result.entity.staffOfficeVos){
									var office = result.entity.staffOfficeVos[i];
									strBuffer += '<tr>';
									strBuffer += '<td>'+office.orgName+'</td>';
									if(office.deptName == null || office.deptName ==''){
										strBuffer += '<td>'+global_null_tip+'</td>';
									}else{
										strBuffer += '<td>'+office.deptName +'</td>';
									}
									if(office.postName == null || office.postName ==''){
										strBuffer += '<td>'+global_null_tip+'</td>';
									}else{
										strBuffer += '<td>'+office.postName +'</td>';
									}
									strBuffer += '<td>'+ layui.util.toDateString(office.startTime,"yyyy-MM-dd HH:mm:ss") +'</td>';
									if(office.endTime == null || office.endTime ==''){
										strBuffer += '<td>'+global_null_tip+'</td>';
									}else{
										strBuffer += '<td>'+ layui.util.toDateString(office.endTime,"yyyy-MM-dd HH:mm:ss") +'</td>';
									}
									if(office.officeState == 1){
										strBuffer += '<td>生效中</td>';
									}else{
										strBuffer += '<td><span class="layui-badge layui-bg-cyan">已失效</span></td>';
									}
									strBuffer += '</tr>';
								}
  								$('#detail_tbody').append(strBuffer);
							}
						}else{
							layer.msg(result.msg,{icon: 7});
			  			}
					}
				});
  			layer.open({
        		type: 1
        		,title: '人员详情'
        		,content: $('#detail_form')
        		,area: layui.admin.screen() < 2 ? ['80%', '620px'] : ['1020px','620px']
      		});
   		}
    	});
    	// 明细table监听toolbar
        table.on('toolbar(LAY-staff-office-detail)', function(obj){
          	var data = obj.data;
          	if(obj.event === 'addOffice'){
          		//开始日期
        	    var insStart = laydate.render({
        	      	elem: '#add-laydate-start'
        	    	,type: 'datetime'
        	      	,done: function(value, date){
            	        //更新结束日期的最小日期
            	        insEnd.config.min = lay.extend({}, date, {
            	          	month: date.month - 1
            	        });
            	        //自动弹出结束日期的选择器
            	       	insEnd.config.elem[0].focus();
        	      	}
        	    });
        	    
        	    //结束日期
        	    var insEnd = laydate.render({
        	      	elem: '#add-laydate-end'
        	    	,type: 'datetime'
        	      	,done: function(value, date){
            	        //更新开始日期的最大日期
            	        insStart.config.max = lay.extend({}, date, {
        	          		month: date.month - 1
        	        	});
        	      	}
        	    });
        	    var orgId = $("#query-select-orgId").val();
        	    $('#add_selectDept').html('');
        	    $('#add_selectPost').html('');
        		//获取公司下的所有部门
        		$.ajax({
        			type : "POST", //提交方式  
        			url : "${ctx}/dept/getAllDeptByOrgId",//路径  
        			data : {
        				"orgId" :orgId
        			},//数据，这里使用的是Json格式进行传输  
        			success : function(result) {//返回数据根据结果进行相应的处理 
        				if(result.flag==true){
        					var strBuffer = "";
        					strBuffer+= '<option value="">请选择</option>';
        					layui.$.each(result.depts, function(i,item){
        						if(item.id != null && item.deptName != null){
        							strBuffer+= '<option value='+item.id+'>'+item.deptName+'</option>';
        						}
        			        });
        					$('#add_selectDept').append(strBuffer);
        					form.render();
        				}else{
        					layer.msg('数据通信异常~',{icon: 0});
        				}
        			} 
        		});
          		layer.open({
    	        	  type: 1
    	        	  ,title: '添加就职岗位'
    	        	  ,content: $('#add_detail_from')
    	        	  ,area: layui.admin.screen() < 2 ? ['80%', '410px'] : ['450px','410px']
    	        	  ,btn: ['确定', '取消']
    	        	  ,btnAlign: 'c'
    	        	  ,yes: function(index, layero){
    	        		  var submitID = 'add_detail_from_submit'
    	            		,submit = layero.contents().find('#'+ submitID);
    	        		  //监听提交
    	        		  form.on('submit('+ submitID +')', function(data){
    	        			  var field = data.field; //获取提交的字段
    	        			  //提交 Ajax 成功后，静态更新表格中的数据
    	        			  $.ajax({
    	        				  type : 'post',
    	        				  url : basePath + '/staff/saveOffice',
    	        				  data: field,
    	        				  success:function(result){
    	        					  if(result.success){
    	        						  table.reload('LAY-staff-office-detail'); //数据刷新
    	        						  layer.close(index); //关闭弹层
    	        						  clean_form("add_detail_from");//清空表单
    	        					  }else{
    	        						  layer.msg(result.msg,{icon: 7});
    	        					  }
    	        				  }
    	              		 });
    	            	});  
    	            submit.trigger('click');
    	          }
    	        });
          	}
        });
    	
     	// 明细table监听操作列
        table.on('tool(LAY-staff-office-detail)', function(obj){
          var data = obj.data;
          if(obj.event === 'delDetail'){
              layer.confirm('确定删除？',{icon:3, title :'提示'}, function(index){
                	$.ajax({
        				type : 'post',
        				url : basePath + '/staff/deleteOffice?id=' + data.id,
        				success:function(result){
        					if(result.success){
        						table.reload('LAY-staff-office-detail'); //数据刷新
        						layer.close(index);
        					}else{
        						layer.msg(result.msg,{icon: 7});
        				  	}
        				}
        			});
                });
            }else if(obj.event === 'editDetail'){
        	    $('#edit_selectDept').html('');
      			//获取公司下的所有部门
      			$.ajax({
      				type : "POST", //提交方式  
      				url : "${ctx}/dept/getAllDeptByOrgId",//路径  
      				data : {
      					"orgId" :data.orgId
      				},//数据，这里使用的是Json格式进行传输  
      				success : function(result) {//返回数据根据结果进行相应的处理 
      					if(result.flag==true){
      						var strBuffer = "";
      						strBuffer+= '<option value="">请选择</option>';
      						layui.$.each(result.depts, function(i,item){
      							if(item.id != null && item.deptName != null){
      								strBuffer+= '<option value='+item.id+'  '
      								if(item.id == data.deptId){
      									strBuffer+= 'selected';
      								}
      								strBuffer+= ' >'+item.deptName+'</option>';
      							}
      				        });
      						$('#edit_selectDept').append(strBuffer);
      						form.render();
      					}else{
      						layer.msg('数据通信异常~',{icon: 0});
      					}
      				} 
      			});
      			var deptId = data.deptId;
      	    	$('#edit_selectPost').html('');
      	    	$.ajax({
      				type : "POST", //提交方式  
      				url : "${ctx}/dept/getPostByDeptId",//路径  
      				data : {
      					"deptId" :deptId
      				},//数据，这里使用的是Json格式进行传输  
      				success : function(result) {//返回数据根据结果进行相应的处理 
      					if(result.flag == true){
      						var strBuffer = "";
      						strBuffer+= '<option value="">请选择</option>';
      						layui.$.each(result.posts, function(i,item){
      							strBuffer+= '<option value='+item.id+'  '
  								if(item.id == data.postId){
  									strBuffer+= 'selected';
  								}
  								strBuffer+= ' >'+item.postName+'</option>';
      				        });
      						$('#edit_selectPost').append(strBuffer);
      						form.render();
      					}else{
      						layer.msg('数据通信异常~',{icon: 0});
      					}
      				},
      				error:function (result){ 
      					layer.msg('数据通信异常~',{icon: 0});
      				}   
      			});
            	form.val('edit_detail_from',data); // 填充原值
            	var startTime = layui.util.toDateString(data.startTime,"yyyy-MM-dd HH:mm:ss");
            	var endTime = layui.util.toDateString(data.endTime,"yyyy-MM-dd HH:mm:ss");
            	//开始日期
        	    var insStart = laydate.render({
        	      	elem: '#edit-laydate-start'
        	    	,type: 'datetime'
        	      	,done: function(value, date){
    	    	        //更新结束日期的最小日期
    	    	        insEnd.config.min = lay.extend({}, date, {
    	    	          	month: date.month - 1
    	    	        });
    	    	        //自动弹出结束日期的选择器
    	    	       	insEnd.config.elem[0].focus();
        	      	}
        	    	,value: startTime
        	    });
        	    
            	if(data.endTime != null && data.endTime != ''){
	        	    //结束日期
	        	    var insEnd = laydate.render({
	        	      	elem: '#edit-laydate-end'
	        	    	,type: 'datetime'
	        	      	,done: function(value, date){
	    	    	        //更新开始日期的最大日期
	    	    	        insStart.config.max = lay.extend({}, date, {
	        	          		month: date.month - 1
	        	        	});
	        	      	}
	        	    	,value: endTime
	        	    });
            	}else{
	        	    //结束日期
	        	    var insEnd = laydate.render({
	        	      	elem: '#edit-laydate-end'
	        	    	,type: 'datetime'
	        	      	,done: function(value, date){
	    	    	        //更新开始日期的最大日期
	    	    	        insStart.config.max = lay.extend({}, date, {
	        	          		month: date.month - 1
	        	        	});
	        	      	}
	        	    });
            	}
            	layer.open({
    	        	  type: 1
    	        	  ,title: '编辑岗位'
    	        	  ,content: $('#edit_detail_from')
    	        	  ,area: layui.admin.screen() < 2 ? ['80%', '410px'] : ['420px','410px']
    	        	  ,btn: ['确定', '取消']
    	        	  ,btnAlign: 'c'
    	        	  ,yes: function(index, layero){
    	        		  var submitID = 'edit_detail_from_submit'
    	            		,submit = layero.contents().find('#'+ submitID);
    	        		  //监听提交
    	        		  form.on('submit('+ submitID +')', function(data){
    	        			  var field = data.field; //获取提交的字段
    	        			  //提交 Ajax 成功后，静态更新表格中的数据
    	        			  $.ajax({
    	        				  type : 'post',
    	        				  url : basePath + '/staff/saveOffice',
    	        				  data: field,
    	        				  success:function(result){
    	        					  if(result.success){
    	        						  table.reload('LAY-staff-office-detail'); //数据刷新
    	        						  layer.close(index); //关闭弹层
    	        						  clean_form("edit_detail_from");//清空表单
    	        					  }else{
    	        						  layer.msg(result.msg,{icon: 7});
    	        					  }
    	        				  }
    	              		 });
    	            	});  
    	            submit.trigger('click');
    	          }
    	        });
            }
        });
    // 按钮事件
    var active = {
      add: function(){
   	    clean_form("add_form");//清空表单
   	 	var orgId = $("#query-select-orgId").val();
   	 	var orgName = $("#query-select-orgId").find("option:selected").text();
   	 	$('#add_orgId').val(orgId);
   	 	$('#add_orgName').val(orgName);
   	 	$('#add_selectDept').html('');
		//获取公司下的所有部门
		$.ajax({
			type : "POST", //提交方式  
			url : "${ctx}/dept/getAllDeptByOrgId",//路径  
			data : {
				"orgId" :orgId
			},//数据，这里使用的是Json格式进行传输  
			success : function(result) {//返回数据根据结果进行相应的处理 
				if(result.flag==true){
					var strBuffer = "";
					strBuffer+= '<option value="">请选择</option>';
					layui.$.each(result.depts, function(i,item){
						if(item.id != null && item.deptName != null){
							strBuffer+= '<option value='+item.id+'>'+item.deptName+'</option>';
						}
			        });
					$('#add_selectDept').append(strBuffer);
					form.render();
				}else{
					layer.msg('数据通信异常~',{icon: 0});
				}
			} 
		});
		form.render();
        layer.open({
          type: 1
          ,title: '添加人员'
          ,content: $('#add_form')
          ,area: layui.admin.screen() < 2 ? ['80%', '350px'] : ['500px','350px']
          ,btn: ['确定', '取消']
          ,btnAlign: 'c'
          ,yes: function(index, layero){
        	  var submitID = 'add_form_submit'
            	,submit = layero.contents().find('#'+ submitID);

              //监听提交
           	  form.on('submit('+ submitID +')', function(data){
              	var field = data.field; //获取提交的字段
              	//提交 Ajax 成功后，静态更新表格中的数据
              	$.ajax({
                  	type : 'post',
					url : basePath + '/staff/save',
                  	data: field,
					success:function(result){
						if(result.success){
              				table.reload('LAY-staff-manage'); //数据刷新
              				clean_form("add_form");//清空表单
              				layer.close(index); //关闭弹层
						}else{
							layer.msg(result.msg,{icon: 7});
					  	}
					}
              	});
            });  
            submit.trigger('click');
          }
        }); 
      }
    }  
    // data-type事件
    $('.layui-btn.layuiadmin-btn-admin').on('click', function(){
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });
    
  	//查询-公司select监听
    form.on('select(query_select_orgId)', function(data){
    	var orgId = data.value;
    	$('#add_orgId').val(orgId);
    	var orgName = data.elem[data.elem.selectedIndex].text;
		$('#add_orgName').val(orgName);
	});
  	
  	//新增岗位-公司select监听
    form.on('select(add_detail_select_orgId)', function(data){
    	var orgId = data.value;
    	$('#add_selectDept').html('');
    	$('#add_selectPost').html('');
    	//获取公司下的所有部门
		$.ajax({
			type : "POST", //提交方式  
			url : "${ctx}/dept/getAllDeptByOrgId",//路径  
			data : {
				"orgId" :orgId
			},//数据，这里使用的是Json格式进行传输  
			success : function(result) {//返回数据根据结果进行相应的处理 
				if(result.flag==true){
					var strBuffer = "";
					strBuffer+= '<option value="">请选择</option>';
					layui.$.each(result.depts, function(i,item){
						if(item.id != null && item.deptName != null){
							strBuffer+= '<option value='+item.id+'>'+item.deptName+'</option>';
						}
			        });
					$('#add_selectDept').append(strBuffer);
					form.render();
				}else{
					layer.msg('数据通信异常~',{icon: 0});
				}
			} 
		});
	});
  	
  	//编辑岗位-公司select监听
    form.on('select(edit_detail_select_orgId)', function(data){
    	var orgId = data.value;
    	$('#edit_selectDept').html('');
    	$('#edit_selectPost').html('');
    	//获取公司下的所有部门
		$.ajax({
			type : "POST", //提交方式  
			url : "${ctx}/dept/getAllDeptByOrgId",//路径  
			data : {
				"orgId" :orgId
			},//数据，这里使用的是Json格式进行传输  
			success : function(result) {//返回数据根据结果进行相应的处理 
				if(result.flag==true){
					var strBuffer = "";
					strBuffer+= '<option value="">请选择</option>';
					layui.$.each(result.depts, function(i,item){
						if(item.id != null && item.deptName != null){
							strBuffer+= '<option value='+item.id+'>'+item.deptName+'</option>';
						}
			        });
					$('#edit_selectDept').append(strBuffer);
					form.render();
				}else{
					layer.msg('数据通信异常~',{icon: 0});
				}
			} 
		});
	});
  	
  	//新增-部门select监听
    form.on('select(add_selectDept)', function(data){
    	var deptId = data.value;
    	$('#add_selectPost').html('');
    	$.ajax({
			type : "POST", //提交方式  
			url : "${ctx}/dept/getPostByDeptId",//路径  
			data : {
				"deptId" :deptId
			},//数据，这里使用的是Json格式进行传输  
			success : function(result) {//返回数据根据结果进行相应的处理 
				if(result.flag == true){
					var strBuffer = "";
					strBuffer+= '<option value="">请选择岗位</option>';
					layui.$.each(result.posts, function(i,item){
						strBuffer+= '<option value='+item.id+'>'+item.postName+'</option>';
			        });
					$('#add_selectPost').append(strBuffer);
					form.render();
				}else{
					layer.msg('数据通信异常~',{icon: 0});
				}
			},
			error:function (result){ 
				layer.msg('数据通信异常~',{icon: 0});
			}   
		});
	});
  	
  	//编辑-部门select监听
    form.on('select(edit_selectDept)', function(data){
    	var deptId = data.value;
    	$('#edit_selectPost').html('');
    	$.ajax({
			type : "POST", //提交方式  
			url : "${ctx}/dept/getPostByDeptId",//路径  
			data : {
				"deptId" :deptId
			},//数据，这里使用的是Json格式进行传输  
			success : function(result) {//返回数据根据结果进行相应的处理 
				if(result.flag == true){
					var strBuffer = "";
					strBuffer+= '<option value="">请选择岗位</option>';
					layui.$.each(result.posts, function(i,item){
						strBuffer+= '<option value='+item.id+'>'+item.postName+'</option>';
			        });
					$('#edit_selectPost').append(strBuffer);
					form.render();
				}else{
					layer.msg('数据通信异常~',{icon: 0});
				}
			},
			error:function (result){ 
				layer.msg('数据通信异常~',{icon: 0});
			}   
		});
	});
  	
  });
  	
	//添加验证
  	function verify(obj,verType){
		var $ = layui.$;
		if(obj.value != null && obj.value != ''){
			$(obj).attr("lay-verify",verType);
		}else{
			$(obj).removeAttr("lay-verify");
		}
	}
  </script>
</body>
</html>

